import { error, EventKeyboard, EventMouse, input, Input, KeyCode, _decorator } from 'cc';
import EventManager from '../core/event-manager';
const { ccclass } = _decorator;

/**
 * 键盘输入事件管理
 */
export class KeyInputManager {
    private _vertical: number = 0;
    private _horizontal: number = 0;
    private _isShift: boolean = false;



    constructor() {
        this.initEvent();
    }
    private initEvent(): void {
        input.on(Input.EventType.KEY_DOWN, this.inputMouseDown, this);
        input.on(Input.EventType.KEY_UP, this.inpotMouseUp, this);
    }
    private inputMouseDown(event: EventKeyboard): void {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this._vertical = 1 << 0;
                break;
            case KeyCode.KEY_S:
                this._vertical = 1 << 1;
                break;
            case KeyCode.KEY_A:
                this._horizontal = 1 << 2;
                break;
            case KeyCode.KEY_D:
                this._horizontal = 1 << 3;
                break;
            case KeyCode.SHIFT_LEFT:
                this._isShift = true;
                break;
            case KeyCode.SPACE:
                // if(this._spaceCall) this._spaceCall();
                break;
            default:
                break;
        }
        this.refreshDriectionState();
    }
    private inpotMouseUp(event: EventKeyboard): void {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                this._vertical = 0;
                break;
            case KeyCode.KEY_S:
                this._vertical = 0;
                break;
            case KeyCode.KEY_A:
                this._horizontal = 0;
                break;
            case KeyCode.KEY_D:
                this._horizontal = 0;
                break;
            case KeyCode.SHIFT_LEFT:
                this._isShift = false;
                break;

            default:
                break;
        }
        this.refreshDriectionState();
    }
    private refreshDriectionState(): void {
        let angle: number = 0;

        angle = this._vertical + this._horizontal;
        if (angle === 0) {
            EventManager.emit("player-move-end");
            return;
        }
        switch (angle) {
            case 1 << 0:  //上
                angle = 0;
                break;
            case 1 << 1:  //下
                angle = 180;
                break;
            case 1 << 2:  //左
                angle = 90;
                break;
            case 1 << 3:   //右
                angle = -90;
                break;
            case (1 << 0) + (1 << 2):  //左上
                angle = 45;
                break;
            case (1 << 0) + (1 << 3):
                angle = -45;
                break;
            case (1 << 1) + (1 << 2):
                angle = 135;
                break;
            case (1 << 1) + (1 << 3):
                angle = -135;
                break;
        }
        let run: boolean = false;
        if (angle <= 60 || angle >= 300) {
            if (this._isShift) {
                run = true;
            }
        }
        console.log("按键按下", angle);
        EventManager.emit("player-move-start", angle, run);
    }

    public destroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.inputMouseDown, this);
        input.off(Input.EventType.KEY_UP, this.inpotMouseUp, this);
    }
}